﻿using Autofac;
using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model.Insur.Request;
using Ewell.BizComponent.Service.Model.InterProxy;
using Ewell.BizComponent.Service.Server;
using Ewell.BizComponent.UI.Core.Model;
using Ewell.BizComponent.UI.SearchItemCom.ViewModel;
using Ewell.Interface.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.UI.SearchItemCom.Views
{
    /// <summary>
    /// SearchItemComView.xaml 的交互逻辑
    /// </summary>
    public partial class SearchItemComView : UserControl
    {
        public static readonly DependencyProperty SearchItemComVmProperty = DependencyProperty
            .Register("searchItemComVm", typeof(SearchItemComVm), typeof(SearchItemComView),
            new FrameworkPropertyMetadata(new SearchItemComVm()));
        public SearchItemComVm searchItemComVm
        {
            get
            {
                if (this.DataContext?.GetType() != typeof(SearchItemComVm))
                    return (SearchItemComVm)GetValue(SearchItemComVmProperty);
                else
                    return this.DataContext as SearchItemComVm;
            }
            set { SetValue(SearchItemComVmProperty, value); }
        }
        public IInsurServer insurServer { get; set; }=Global.Container.Resolve<IInsurServer>();
        public SearchItemComView()
        {
            InitializeComponent();
            this.DataContext = searchItemComVm;
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key is Key.Enter or Key.Space or Key.Up or Key.Down or Key.Left or Key.Right) return;
            ComboBox comboBox = sender as ComboBox;
            DataSelectItemVm dataSelectItem = comboBox.Tag as DataSelectItemVm;
            List<DataSelectRequest> alreadySelectDataList = new List<DataSelectRequest>();
            foreach (var dataSelect in this.searchItemComVm.dataSelectItemList)
            {
                //if (dataSelect.mustFlag != "1") continue;
                if (dataSelect.componentTypeCoding == "1")
                {
                    if(dataSelect.selectedBasicData==null) continue;
                    alreadySelectDataList.Add(dataSelect.selectedBasicData?.CopyProperties<DataSelectRequest>());
                }
                else if (dataSelect.componentTypeCoding == "2")
                {
                    alreadySelectDataList.Add(new DataSelectRequest()
                    {
                        basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                        basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                        basicDataId= dataSelect.textBasicData,
                        basicDataName=dataSelect.textBasicData,
                        basicDataNo=dataSelect.textBasicData,
                        basicDataExtInfo=dataSelect.textBasicData,
                    });
                }else if(dataSelect.componentTypeCoding == "3")
                {
                    alreadySelectDataList.Add(new DataSelectRequest()
                    {
                        basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                        basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                        basicDataId = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                        basicDataName = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                        basicDataNo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                        basicDataExtInfo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                    });
                }else if (dataSelect.componentTypeCoding == "4")
                {
                    alreadySelectDataList.Add(new DataSelectRequest()
                    {
                        basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                        basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                        basicDataId = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                        basicDataName = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                        basicDataNo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                        basicDataExtInfo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                    });
                }
            }
            var basicDatas = insurServer.SearchDataSelectItem(new RequestHeader() { interfaceId = long.Parse(searchItemComVm.selectedPatientType.interfaceId), cardInfo = this.searchItemComVm.readCardResponse, alreadySelectDataList = alreadySelectDataList }, new DataSelectItemQueryRequest() { basicDataDictTypeId = long.Parse(dataSelectItem.basicDataDictTypeId), basicDataDictTypeNo = dataSelectItem.basicDataDictTypeNo, currentBizParams = this.searchItemComVm.currentBizParams, currentBizTransactionCoding = this.searchItemComVm.outPreSettlementRequest.settlementTypeCode == "1" ? "101" : "201", keyword = comboBox.Text });
            var dataSelectItemVm = this.searchItemComVm.dataSelectItemList.FirstOrDefault(f => f.basicDataDictTypeId == dataSelectItem.basicDataDictTypeId);
            if (dataSelectItemVm != null)
            {
                dataSelectItemVm.basicDataList = new List<BasicDataVm>();
                if (basicDatas != null && basicDatas.Count() > 0)
                {
                    foreach (var item in basicDatas)
                    {
                        dataSelectItemVm.basicDataList.Add(new BasicDataVm()
                        {
                            basicDataDictTypeId = dataSelectItemVm.basicDataDictTypeId,
                            basicDataDictTypeNo = dataSelectItemVm.basicDataDictTypeNo,
                            basicDataId = item.basicDataId,
                            basicDataNo = item.basicDataNo,
                            basicDataName = item.basicDataName,
                            basicDataExtInfo = item.basicDataExtInfo,
                            pinyin = item.pinyin,
                            wubi = item.wubi,
                        });
                    }
                }
            }
            comboBox.ItemsSource = dataSelectItemVm.basicDataList;
            comboBox.IsDropDownOpen = true;
        }

        private void ComboBox_DropDownClosed(object sender, EventArgs e)
        {
            if (sender is ComboBox)
            {
                if ((sender as ComboBox).SelectedItem != null)
                {
                    (sender as ComboBox).Text = ((sender as ComboBox).SelectedItem as BasicDataVm)?.basicDataName;
                }
            }
        }

        private void ComboBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(string.IsNullOrWhiteSpace((sender as ComboBox).Text))
            {
                DataSelectItemVm dataSelectItem = (sender as ComboBox).Tag as DataSelectItemVm;
                List<DataSelectRequest> alreadySelectDataList = new List<DataSelectRequest>();
                foreach (var dataSelect in this.searchItemComVm.dataSelectItemList)
                {
                    //if (dataSelect.mustFlag != "1") continue;
                    if (dataSelect.componentTypeCoding == "1")
                    {
                        if (dataSelect.selectedBasicData == null) continue;
                        alreadySelectDataList.Add(dataSelect.selectedBasicData?.CopyProperties<DataSelectRequest>());
                    }
                    else if (dataSelect.componentTypeCoding == "2")
                    {
                        alreadySelectDataList.Add(new DataSelectRequest()
                        {
                            basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                            basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                            basicDataId = dataSelect.textBasicData,
                            basicDataName = dataSelect.textBasicData,
                            basicDataNo = dataSelect.textBasicData,
                            basicDataExtInfo = dataSelect.textBasicData,
                        });
                    }
                    else if (dataSelect.componentTypeCoding == "3")
                    {
                        alreadySelectDataList.Add(new DataSelectRequest()
                        {
                            basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                            basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                            basicDataId = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                            basicDataName = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                            basicDataNo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                            basicDataExtInfo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd"),
                        });
                    }
                    else if (dataSelect.componentTypeCoding == "4")
                    {
                        alreadySelectDataList.Add(new DataSelectRequest()
                        {
                            basicDataDictTypeId = dataSelect.basicDataDictTypeId,
                            basicDataDictTypeNo = dataSelect.basicDataDictTypeNo,
                            basicDataId = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                            basicDataName = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                            basicDataNo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                            basicDataExtInfo = dataSelect.timeBasicData?.ToString("yyyy-MM-dd HH:mm:ss"),
                        });
                    }
                }
                var basicDatas = insurServer.SearchDataSelectItem(new RequestHeader() { interfaceId = long.Parse(searchItemComVm.selectedPatientType.interfaceId), cardInfo = this.searchItemComVm.readCardResponse, alreadySelectDataList = alreadySelectDataList }, new DataSelectItemQueryRequest() { basicDataDictTypeId = long.Parse(dataSelectItem.basicDataDictTypeId), basicDataDictTypeNo = dataSelectItem.basicDataDictTypeNo, currentBizParams = this.searchItemComVm.currentBizParams, currentBizTransactionCoding = this.searchItemComVm.outPreSettlementRequest.settlementTypeCode == "1" ? "101" : "201", keyword = string.Empty });
                var dataSelectItemVm = this.searchItemComVm.dataSelectItemList.FirstOrDefault(f => f.basicDataDictTypeId == dataSelectItem.basicDataDictTypeId);
                if (dataSelectItemVm != null)
                {
                    dataSelectItemVm.basicDataList = new List<BasicDataVm>();
                    if (basicDatas != null && basicDatas.Count() > 0)
                    {
                        foreach (var item in basicDatas)
                        {
                            dataSelectItemVm.basicDataList.Add(new BasicDataVm()
                            {
                                basicDataDictTypeId = dataSelectItemVm.basicDataDictTypeId,
                                basicDataDictTypeNo = dataSelectItemVm.basicDataDictTypeNo,
                                basicDataId = item.basicDataId,
                                basicDataNo = item.basicDataNo,
                                basicDataName = item.basicDataName,
                                basicDataExtInfo = item.basicDataExtInfo,
                                pinyin = item.pinyin,
                                wubi = item.wubi,
                            });
                        }
                    }
                }
            }
        }
    }
}
